package com.hp.modules.sys.service.impl;

import com.github.pagehelper.Page;
import com.hp.common.base.CommonPage;
import com.hp.common.base.WhereParam;
import com.hp.common.base.YesNoEnum;
import com.hp.common.tk.ConditionUtil;
import com.hp.mapper.SysConfigMapper;
import com.hp.modules.sys.dto.SysConfigPageParam;
import com.hp.modules.sys.dto.SysConfigParam;
import com.hp.modules.sys.entity.SysConfig;
import com.hp.modules.sys.service.SysConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import java.util.Date;
import java.util.List;

/**
 * @Author: mcfan
 * @Description: TODO 业务接口实现层
 * @Date: 2021/9/7 16:10
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(SysConfigParam param) {
        Date now = new Date();
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(param, sysConfig);
        sysConfig.setCreateTime(now);
        sysConfig.setUpdateTime(now);
        sysConfig.setIsDeleted(YesNoEnum.NO);
        return sysConfigMapper.insertSelective(sysConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(SysConfigParam param) {
        Date now = new Date();
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(param, sysConfig);
        sysConfig.setUpdateTime(now);
        return sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int remove(List<Long> ids) {
        Date now = new Date();
        SysConfig upSysConfig = new SysConfig();
        upSysConfig.setUpdateTime(now);
        Condition condition = new Condition(SysConfig.class);
        condition.createCriteria().andIn("id", ids);
        //更新时间
        sysConfigMapper.updateByConditionSelective(upSysConfig, condition);
        //逻辑删除
        return sysConfigMapper.deleteByCondition(condition);
    }

    @Override
    public SysConfig get(Long id) {
        return (SysConfig) sysConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonPage<SysConfig> list(SysConfigPageParam param) {
        Page<SysConfig> page = param.buildPage(true);
        List<WhereParam> whereParams = param.getWhereParams();
        if (null == whereParams || whereParams.isEmpty()) {
            SysConfig sysConfig = new SysConfig();
            sysConfigMapper.select(sysConfig);
        } else {
            sysConfigMapper.selectByCondition(ConditionUtil.buildCondition(SysConfig.class, whereParams));
        }
        return CommonPage.toPage(page);
    }
}
